import { defineStore } from 'pinia'
import type { PatrolClassEvaluation } from '../types/PatrolClassEvaluation'

/**
 * 巡课评价存储模块
 * 负责管理巡课评价数据的全局状态
 */
export const usePatrolClassEvaluationStore = defineStore('patrolClassEvaluation', {
    state: () => ({
        // 所有评价记录
        evaluations: [] as PatrolClassEvaluation[],
        // 当前查看的评价
        currentEvaluation: null as PatrolClassEvaluation | null,
        // 加载状态
        loading: false,
        // 统计筛选条件
        statsFilter: {
            startDate: null as string | null,
            endDate: null as string | null,
            classStatus: null as number | null
        }
    }),
    actions: {
        /**
         * 获取所有巡课评价
         */
        async fetchAllEvaluations() {
            this.loading = true
            try {
                // this.evaluations = await evaluationApi.getAll()
            } finally {
                this.loading = false
            }
        },

        /**
         * 设置统计筛选条件
         * @param filter - 筛选条件
         */
        setStatsFilter(filter: {
            startDate?: string | null,
            endDate?: string | null,
            classStatus?: number | null
        }) {
            this.statsFilter = { ...this.statsFilter, ...filter }
        },

        /**
         * 提交新的巡课评价
         * @param newEvaluation - 新评价对象
         */
        async submitEvaluation(newEvaluation: Omit<PatrolClassEvaluation, 'patrolClassEvaluationId'>) {
            this.loading = true
            try {
                // const created = await evaluationApi.submit(newEvaluation)
                // this.evaluations.unshift(created)
            } finally {
                this.loading = false
            }
        }
    },
    getters: {
        /**
         * 获取筛选后的评价列表
         */
        filteredEvaluations(state): PatrolClassEvaluation[] {
            let result = [...state.evaluations]
            if (state.statsFilter.startDate) {
                result = result.filter(e => e.roundTime >= state.statsFilter.startDate!)
            }
            if (state.statsFilter.endDate) {
                result = result.filter(e => e.roundTime <= state.statsFilter.endDate!)
            }
            if (state.statsFilter.classStatus) {
                result = result.filter(e => e.classStatus === state.statsFilter.classStatus)
            }
            return result
        },

        /**
         * 获取各班级的平均评分
         */
        classAverages(state) {
            const classMap = new Map<number, { sum: number, count: number }>()
            state.evaluations.forEach(e => {
                if (!classMap.has(e.classId)) {
                    classMap.set(e.classId, { sum: 0, count: 0 })
                }
                const entry = classMap.get(e.classId)!
                entry.sum += e.contentClarity || 0
                entry.count++
            })

            return Array.from(classMap.entries()).map(([classId, { sum, count }]) => ({
                classId,
                average: sum / count
            }))
        }
    }
})
